package com.tuanzi.loan.web.bean.statistics;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.constants.DictionaryCode;
import com.tuanzi.loan.business.entity.common.CommProductChannel;
import com.tuanzi.loan.business.service.common.CommProductChannelService;
import com.tuanzi.loan.business.service.common.CommProductIdService;
import com.tuanzi.loan.business.service.statistics.ActivityParticipateStatService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.statistics.ActivityParticipateStatSearchRequest;
import com.tuanzi.loan.vo.response.statistics.ActivityParticipateStatSearchResponse;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.statistics.ActivityParticipateStatDataModel;
import com.tuanzi.server.loan_base.model.bean.common.CommAdvertise;
import com.tuanzi.server.loan_base.model.bean.common.CommPlatform;
import com.tuanzi.server.loan_base.model.bean.common.CommUserType;
import com.tuanzi.server.loan_base.model.db.LoanProductInfo;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.joda.time.LocalDate;
import org.primefaces.model.CheckboxTreeNode;
import org.primefaces.model.StreamedContent;
import org.primefaces.model.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 活动参与统计
 * @author JinRuiyang 2018/1/26 10:34:21
 */
@Debug
@Data
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class ActivityParticipateStatBean extends JSFListingBean<ActivityParticipateStatSearchRequest, ActivityParticipateStatSearchResponse> {

    @Autowired
    private ActivityParticipateStatService activityParticipateStatService;
    @Autowired
    private CommProductChannelService commProductChannelService;
    @Autowired
    private CommProductIdService commProductIdService;

    private StreamedContent xlsFile;
    private List<LoanProductInfo> activityList;
    private List<CommAdvertise> adList;
    private List<CommPlatform> platformList;
    private List<CommUserType> userTypeList;
    private TreeNode productTreeNode;
    private TreeNode channelTreeNode;

    @PostConstruct
    private void init() {
        initList();
        initLazyDataModel();
    }

    private void initList(){
        activityList = activityParticipateStatService.getActivity();
        adList = activityParticipateStatService.getAD();
        platformList = activityParticipateStatService.getPlatform();
        userTypeList = activityParticipateStatService.getUserType();
        productTreeNode = commProductIdService.createProductTreeNode(null);
        channelTreeNode = commProductChannelService.createChannelGroupTreeNode(null);
        //为渠道树形增加级全部选项
        CommProductChannel tempCommChannel = new CommProductChannel();
        tempCommChannel.setChannelName("汇总统计");
        tempCommChannel.setChannelCode(-9999);
        CheckboxTreeNode checkboxTreeNode = new CheckboxTreeNode(tempCommChannel);
        channelTreeNode.getChildren().add(0,checkboxTreeNode);
    }

    public void onFlushCache(){
        activityParticipateStatService.cleanCache();
        initList();
        addInfoMessage("缓存更新成功!");
    }

    public void onReset() {
        initLazyDataModel();
    }

    public void onRefresh(){
        lazyDataModel = new ActivityParticipateStatDataModel($);
        addSuccessfullyMessage();
    }

    private void initLazyDataModel() {
        $ = new ActivityParticipateStatSearchRequest();
        // 初始化默认当前日期
        Date date = LocalDate.now().toDate();
        $.setStartTime(date);
        $.setEndTime(date);

        // 初始化默认选择全部
        List<Integer> defaultInt = new ArrayList<Integer>();
        defaultInt.add(-1000);
        $.setUserType(defaultInt);
        $.setAdId(defaultInt);
        
        List<String> defaultPlatForm = new ArrayList<String>();
        defaultPlatForm.add("-1000");
        $.setPlatform(defaultPlatForm);
        
        List<Long> defaultActivity = new ArrayList<Long>();
        defaultActivity.add(-1000L);
        $.setActivityId(defaultActivity);

        this.channelTreeNode.setSelected(false);
        this.productTreeNode.setSelected(false);
        lazyDataModel = new ActivityParticipateStatDataModel($);
    }


    @Override
    protected List<String> getDictionaryCodes() {
        return Lists.newArrayList(DictionaryCode.CHANNEL_GROUP,
                DictionaryCode.LARGE_COMMON_PRODUCT, DictionaryCode.RECOMMEND_POOL_TYPE);
    }
}




